<?php

set_time_limit(0);
date_default_timezone_set('PRC');

/**
 * cnBeat Splider
 */
class Splider
{
    private $link    = null;

    private $cookie  = null;

    private $query   = null;

    private $base    = null;

    private $referer = null;

    private $store   = 0;
	
	private $fail    = 0;

    private $current = 0;

    public function __construct()
    {
        $this->base    = 'http://www.cnbeta.com/home/more';
        $this->referer = 'http://www.cnbeta.com/';

        $this->link   = $this->build_db();
        $this->cookie = $this->build_cookie();
        $this->query  = $this->parse_query();
    }

    // 链接数据库
    private function build_db()
    {
        $link = mysqli_connect("127.0.0.1:3306", "root", "", "demo");
        if (empty($link)) {
            die('Could not connect: ' . mysqli_error($link));
        }
		mysqli_set_charset($link,"utf8");
        echo '1 ';
        return $link;
    }

    // 构建cookie
    private function build_cookie()
    {
        $cookie = file_get_contents('./cookie');
        if (empty($cookie)) {
            die('no cookie');
        }
        echo '2 ';
        return $cookie;
    }

    // 解析查询
    private function parse_query()
    {
        $args = parse_url(file_get_contents('query'), PHP_URL_QUERY);
        $args = explode('&', $args);
        foreach ($args as $vo) {
            if (empty($vo)) {
                continue;
            }
            list($key, $value) = explode('=', $vo);
            if ($key == '_csrf') {
                $value = urldecode($value);
            }
            $para[$key] = $value;
        }
        if (empty($para)) {
            die('no query');
        }
        echo '3 ';
        return $para;
    }

    // 构建查询
    private function build_query($i)
    {
        $query          = $this->query;
        $query['page'] += $i;
        $query['_']    += $i;
        $query = http_build_query($query);
        return $query;
    }

    // 插入数据库
    private function store_db($list)
    {
        if (empty($list)) {
            return false;
        }
        $query = "INSERT INTO articles (title, keywords, thumb, summary, content, comments, url_show, create_at, update_at)"
            ." VALUES (?,?,?,?,?,?,?,?,?) ";
        $stmt  = mysqli_prepare($this->link, $query);
        mysqli_stmt_bind_param($stmt, "sssssisii", $title, $keywords, $thumb, $summary, $content, $comments, $url_show, $create_at, $update_at);
        foreach ($list as $data) {
            $title     = $data['title'];
            $keywords  = $data['keywords'];
            $thumb     = $data['thumb'];
            $summary   = $data['hometext'];
            $content   = $this->getDetail($data['url_show']);
            $comments  = $data['comments'];
            $url_show  = $data['url_show'];
            $create_at = strtotime($data['inputtime']);
            $update_at = $data['updatetime'];
            $insertID  = mysqli_stmt_execute($stmt);
            if ($insertID) {
                $this->store += 1;
            }
        }

        return true;
    }

    // 请求html
    private function getHtml($query)
    {
        $ch  = curl_init();
        $url = $this->base.'?'.$query;

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_COOKIE, $this->cookie);
        curl_setopt($ch, CURLOPT_REFERER, $this->referer);

        $output = curl_exec($ch);

        if (curl_errno($ch)) {
            $log = 'Curl error: ' . curl_error($ch).PHP_EOL;
			$this->log($log);
        }
        curl_close($ch);
        return $output;
    }

    // 获取文章详情
    public function getDetail($url)
    {
        if (empty($url)) {
            return '';
        }
        $subject = file_get_contents($url);
        $pattern = "/<div class=\"article-content\">(.*)<\/div>/iUs";
        if (empty($subject) == false) {
            preg_match($pattern, $subject, $matches);
            return trim($matches[1]);
        }
        return '';
    }

    // 运行
    public function start($count = 1)
    {
		$begin_time = time();
        echo 'START ...'.PHP_EOL.PHP_EOL;
        for ($i = 0; $i < $count; $i++) {

            $this->_sleep();

            $query = $this->build_query($i);
            $resp  = $this->getHtml($query);
            $data  = json_decode($resp, true);
            $list  = $data['result']['list'];

            if (empty($list)) {
                $this->log($resp);
				$this->fail++;
            } else {
                $this->store_db($list);
            }
            echo sprintf('%s store:%d', $data['state'], $this->store);
            echo PHP_EOL.PHP_EOL;
        }
        echo sprintf('END ^_^ total:%d fail:%d store:%d', $count, $this->fail, $this->store);
        echo PHP_EOL;
        echo sprintf('time %d ', $this->fmt_time(time() - $begin_time));
		echo PHP_EOL;
        mysqli_close($this->link);
        $this->store   = 0;
        $this->current = 0;
		$end_time = time();
    }
	
	// 格式化秒为可读状态
	public function fmt_time($second)
	{
		$day    = $second / 3600 / 24;
		$hour   = $second / 3600 % 24;
		$minute = $second % 3600 / 60;
		$second = $second % 60;
		return sprintf('day %d, hour %d, minute %d, second %d', $day, $hour, $minute, $second);
	}
	
	// 记录日志
	private function log($log)
	{
		$log = $this->current.' ' . date('Y-m-d H:i:s') . ' ' . $log;
		file_put_contents('./log', $log, FILE_APPEND);
	}

    // 等待时间
    private function _sleep()
    {
        $i    = 0;
        $wait = rand(1, 5);
        $this->current += 1;
        echo sprintf('No.%d wait %02ds:', $this->current, $wait);
        sleep(1);
        while ($i++ < $wait) {
            echo ' *';
            sleep(1);
        }
        echo PHP_EOL;
        echo 'loading...'.PHP_EOL;
    }
	

    // 静态方法好操作
    public static function run($count = 1)
    {
        $s = new Splider();
        $s->start($count);
    }
}

Splider::run(5);